package com.cskaoyan.crud;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * 创建日期: 2022/10/08 10:04
 *
 * @author ciggar
 */
public class JDBCSelectDemo {

    public static void main(String[] args) throws SQLException {

        // 1. 注册驱动
        DriverManager.registerDriver(new Driver());

        // 2. 获取连接 (返回的应该是接口的实现类的实例对象 )(com.mysql.jdbc.JDBC4Connection)
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/45th?characterEncoding=utf8&useSSL=false","root","123456");


        // 3. 获取statement对象
        Statement statement = connection.createStatement();


        // 4. 执行sql语句
        ResultSet resultSet = statement.executeQuery("select * from stu");

        // 5. 解析结果
        while (resultSet.next()) {

            int id2 = resultSet.getInt("id");
            String name2 = resultSet.getString("name");
            String clazz2 = resultSet.getString("clazz");

            System.out.println("id:" + id2 + ", name:" + name2 + ", clazz:" + clazz2);

        }

        // 6. 断开连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
